package com.sundablog.utlis;


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.CentralProcessor.TickType;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.util.FormatUtil;
import oshi.util.Util;

/**
 * Java系统监控测试类
 * 
 * @ClassName: SystemInfoTest
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: 哒哒
 * @date: 2018年3月1日 下午5:33:51
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public class SystemUtils {
	
	/**
	 * 获取系统内存
	 * @Title: Memory   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param memory
	 * @param: @return      
	 * @return: Map<String,String>      
	 * @throws
	 */
	public static Map<String, String> Memory(GlobalMemory memory) {
		long available = memory.getAvailable();
		long total = memory.getTotal();
		double aa = (available / (double) total) * 100;
		int percentage = (int) aa;
		Map<String, String> map = new HashMap<>();
		map.put("available", FormatUtil.formatBytes(memory.getAvailable()));
		map.put("total", FormatUtil.formatBytes(memory.getTotal()));
		map.put("Percentage", String.valueOf(percentage));
		return map;
	}
	/**
	 * 获取系统cpu百分百
	 * @Title: Cpu   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param processor
	 * @param: @return      
	 * @return: int      
	 * @throws
	 */
	public static int Cpu(CentralProcessor processor) {
		 
	        Util.sleep(1000);
	    
		return  (int) (processor.getSystemCpuLoadBetweenTicks() * 100);
	}

	private static void printCpu(CentralProcessor processor) {
        System.out.println("Uptime: " + FormatUtil.formatElapsedSecs(processor.getSystemUptime()));
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        System.out.println("CPU, IOWait, and IRQ ticks @ 0 sec:" + Arrays.toString(prevTicks));
        // Wait a second...
        Util.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        System.out.println("CPU, IOWait, and IRQ ticks @ 1 sec:" + Arrays.toString(ticks));
        long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
        long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
        long sys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
        long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
        long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
        long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
        long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
        long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
        long totalCpu = user + nice + sys + idle + iowait + irq + softirq + steal;
        System.out.format(
                "User: %.1f%% Nice: %.1f%% System: %.1f%% Idle: %.1f%% IOwait: %.1f%% IRQ: %.1f%% SoftIRQ: %.1f%% Steal: %.1f%%%n",
                100d * user / totalCpu, 100d * nice / totalCpu, 100d * sys / totalCpu, 100d * idle / totalCpu,
                100d * iowait / totalCpu, 100d * irq / totalCpu, 100d * softirq / totalCpu, 100d * steal / totalCpu);
        System.out.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
        System.out.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);
        double[] loadAverage = processor.getSystemLoadAverage(3);
        System.out.println("CPU load averages:" + (loadAverage[0] < 0 ? " N/A" : String.format(" %.2f", loadAverage[0]))
                + (loadAverage[1] < 0 ? " N/A" : String.format(" %.2f", loadAverage[1]))
                + (loadAverage[2] < 0 ? " N/A" : String.format(" %.2f", loadAverage[2])));
        // per core CPU
        StringBuilder procCpu = new StringBuilder("CPU load per processor:");
        double[] load = processor.getProcessorCpuLoadBetweenTicks();
        for (double avg : load) {
            procCpu.append(String.format(" %.1f%%", avg * 100));
        }
        System.out.println(procCpu.toString());
    }
	
	public static void main(String[] args) {
		SystemInfo si = new SystemInfo();

		HardwareAbstractionLayer hal = si.getHardware();
		System.err.println(Cpu(hal.getProcessor()));
		printCpu(hal.getProcessor());
	
	}

}
